package com.qianbao.controller.v1.address;


import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.svresut.Result;
import com.qianbao.components.svversion.ApiVersion;
import com.qianbao.domain.address.AddressEntity;
import com.qianbao.service.address.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Controller
@ApiVersion(1)
@RequestMapping("/addresscenter/{version}/")
public class AddressController {

    @Autowired
    private AddressService service;


    @RequestMapping("/save-address")
    @ResponseBody
    public Result saveUserAddress(@Valid AddressEntity entity){
        try {
            service.saveAddress(entity);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }


    @RequestMapping("/update-address")
    @ResponseBody
    public Result updateUserAddress(@Valid AddressEntity entity){
        try {
            service.updateAddress(entity);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }

    @RequestMapping("/delete-address")
    @ResponseBody
    public Result deleteUserAddress(String addressId){
        try {
            service.deleteAddress(addressId);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }


    @RequestMapping("/find-all-address")
    @ResponseBody
    public Result findUserAddress(String customId){
        try {
            List<AddressEntity> addressList = service.findAllAddressList(customId);
            HashMap<String,List> map = new HashMap<>();
            map.put("list",addressList);
            return Result.success(map);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }





}
